<?php

namespace App\Http\Controllers\Api;

use App;
use App\Util\Common;
use App\Util\GetMac;
use App\Util\SqlServerUtil;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use function Symfony\Component\Debug\header;
use Illuminate\Support\Facades\Storage;
use Excel;
use Illuminate\Support\Facades\Cache;

class SmsController extends CommonController
{
    protected $response = [
    		"Error" => 0,
    		"Message" => "Success!",
    		"Data" => []
	];
    
	private $ERPUrl ="http://xun2025.com:7080/api/sms/sendsms";
	private $ERPDXUrl ="http://xun2025.com:7080/api/sms/senddxsms";
	private $MODE = "ycbj";
    /**
    * 短信 发送
    * @date: 2022年12月12日 下午5:35:27
    * @author: juyi
    */
	public function sendSms(Request $request){
		$fid = $request->fid ;//用户名id 
    	$fcontent = $request->fcontent ;
    	$fbillno = $request->fbillno ; //流水号
    	$res = SqlServerUtil::execTosql($this->SqlServer,"select fphone from gz_admin where 1=1 and id in ({$fid})  and fphone !='' ");
    	$fcontent = $fbillno .' 。'. $fcontent ;
    	if($res){
    		foreach ($res as $k=>$v)
    		{
//     			if(!check_phone($v)){
//     				showMsg(400,'手机号不对'); 
//     				break; 
//     			}
    			$this->sendSmsData($v['fphone'] , $fcontent) ;
    		}
    		
    	}
    
    	 showMsg(0,'成功');
    }
    
    
    public function  sendSmsData($fphone , $fcontent){
    	$mode = $this->MODE ; //模块
    	$getData=[
    			'fphone'=> $fphone,
    			'mode'=> $mode,
    			'param1'=> $fcontent,
    	];
//     	dd($getData);
    	$sep ='?';
    	$getDataTemp = http_build_query($getData) ;
    	$send = $this->ERPUrl.$sep.$getDataTemp  ;
    	$result = httpRequest($send, 'get');
    	$result = json_decode($result,true) ;
    	if($result['code'] == 200 ){
    		return  ['code'=>200,'message'=>$result['message']];
    	}
    	return  ['code'=>400,'message'=>$result['message'].' 请一会 再试！！！' ];
    }
     
    /**
    * 短信
    * @date: 2023年8月25日 下午4:43:14
    * @author: juyi
    * @param: variable
    * @return:
    */
    public function sendDXSmsData($fphone , $fcontent){
    	$getData=[
    			'fphone'=> $fphone,
    			'param1'=> $fcontent,
    	];
    	$sep ='?';
    	$getDataTemp = http_build_query($getData) ;
    	$send = $this->ERPDXUrl.$sep.$getDataTemp  ;
    	$result = httpRequest($send, 'get');
    	$result = json_decode($result,true) ;
    	if($result['code'] == 200 ){
    		return  ['code'=>200,'message'=>$result['message']];
    	}
    	return  ['code'=>400,'message'=>$result['message'].' 10分钟后过期， 请一会 再试！！！' ];
    }
	
    
    /**
    *  登录页  短信  Cache::put('key', 'value', $minutes);
    * @date: 2023年8月23日 下午4:18:36
    * @author: juyi
    */
    public function getsmsCode(Request $request){
    	$name = $request->name ; 
    	$phone = $request->phone ; 
    	if(strtolower($name) == 'manager'){
    		showMsg(400,'Manager账号，无法修改!');
    	}
    	$sql = " select username , fphone ,status  from gz_admin where 1=1 and username = '{$name}'  and fphone ='{$phone}' " ; 
    	$res = SqlServerUtil::execTosql($this->SqlServer,$sql ,2);
    	// status 为 0 ，已禁用
    	if(empty($res)){
    		showMsg(400,'账号和手机号未绑定，请联系管理员!');
    	}
    	if( $res['status']<1 ){
    		showMsg(400,'账号已禁用，请联系管理员!');
    	}
    	
    	$fcontent = rand(1000,9999);
    	$fphone = $phone;
    	
    	if (!(Cache::get('smsCode:'.$phone ) )) {
    		$smsin = $this->sendDXSmsData( $fphone, $fcontent) ;
    		if($smsin['code'] == 400 ){
    			showMsg(400,$smsin['message']);
    		}
    		// 缓存不存在
    		Cache::put('smsCode:'.$phone, $fcontent, 10); // 缓存10分钟
    	}else{
    		showMsg(400,'验证码发送频繁， 10分钟后过期，请一会儿再试。');
    	}
    	showMsg(0,'成功');
    }
    
    /**
    * 修改Sms密码 
    * @date: 2023年8月23日 下午6:51:28
    * @author: juyi
    */
    public function UpdateSmsPass(Request $request){
    	$data = $request->all(); 
    	$phone = $data['phone'] ;
    	$name = $data['name'] ;
    	$pass = $data['pass'] ;
    	if(strtolower($name) == 'manager'){
    		showMsg(400,'Manager账号，无法修改!');
    	}
    	
    	$basepass = base64_encode($pass);
    	$update_time = getCurrentTime();
    	$sql = " select username , fphone ,status  from gz_admin where 1=1 and username = '{$name}'  and fphone ='{$phone}' " ;
    	$res = SqlServerUtil::execTosql($this->SqlServer,$sql ,2);
    	if(empty($res)){
    		showMsg(400,'手机号 或账号绑定不正确，请联系管理员!');
    	}
    	if ( Cache::has('smsCode:'.$phone )  ) {
    		//验证码是否正确
    		if( $data['fcode']  != Cache::get('smsCode:'.$phone ) ){
    			showMsg(400,'验证码不正确，请检查!');
    		}
    		//修改密码   sqlexecTosql
    		$sql = " update gz_admin  set  password = '{$basepass}' , update_time = '{$update_time}' where 1=1 and username = '{$name}'  and fphone ='{$phone}' " ;
    		$res = SqlServerUtil::sqlexecNo($this->SqlServer,$sql ); 
    		
    		showMsg(0,'成功');
    	}else{
    		//不存在
    		showMsg(400,'短信验证码过期或错误，请重试。');
    	}
    }
    
    

}
